var app = getApp();
var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
var common = require('../../utils/common.js');
var MPage = require('../MPage.js');


Page(common.extendPage(MPage, {
    data: {
        winHei: 0,
        imgList: [],
        starNum: 5,
        content:''
    },

    onLoad(options) {
        this._options = options;
        this.setData({
            winHei: app.getWinHei()
        });
    },

    //上传图片
    _upload() {
        var _self = this;

        wx.chooseImage({
            success: function(res) {
                var tempFilePaths = res.tempFilePaths || [];
                var imgList = _self.data.imgList;
                var oldLen = imgList.length;
                var curImgList = [];
                var uploadNum = 0;

                wx.showLoading({
                    title: '上传中'
                });
                tempFilePaths.forEach((imgUrl, i) => {
                    ((i) => {
                        wx.uploadFile({
                            url: app.config.apiUrl + '/openapi/UploadImg/upload',
                            filePath: imgUrl,
                            name: 'image',
                            header: {
                                'content-type': 'multipart/form-data',
                            },
                            success: (res) => {
                                var d = res.data;

                                if (typeof(d) == 'string') {
                                    d = JSON.parse(d);
                                }
                                if (d.code == 0) {
                                    var imgUrl = d.result.url;

                                    if (imgUrl) {
                                        imgList[oldLen + i] = imgUrl;
                                        _self.setData({
                                            imgList
                                        });
                                    }
                                }
                                uploadNum++;
                                if (uploadNum >= tempFilePaths.length) { //上传完毕
                                    wx.hideLoading();
                                }
                            }
                        })
                    })(i);
                })
            }
        })
    },

    _tapStar(e) {
        const starLen = 175;
        const eveStarLen = 175 / 5;
        var {
            pageX,
        } = e.touches[0];
        var {
            offsetLeft,
        } = e.currentTarget;
        var starNum = Math.ceil((pageX - offsetLeft) / eveStarLen);

        if (starNum <= 1) {
            starNum = 1;
        }
        if (starNum >= 5) {
            starNum = 5;
        }
        this.setData({
            starNum
        })
    },

    _contentChange(e) {
        this.setData({
            content: e.detail.value
        })
    },

    _submit() {
        var {
            content,
            starNum,
            imgList
        } = this.data;
        var {
            goodsId,
            orderId
        } = this._options;
        if (!content){
            util.alert('请输入您的评价~');
            return false;
        }

        ajax.P('/openapi/CGoods/doComment', {
            goodsId,
            orderId,
            content,
            score: starNum,
            imgUrl: imgList.join('|')
        }, function(data) {
            util.alert('评论成功', () => {
                wx.redirectTo({
                    url: '/pages/orderDetail/orderDetail?orderId=' + orderId
                })
            })
        });
    }


}));